package com.example.myletcodelearing.one

import com.example.myletcodelearing.kmp.KmpTest

/**
 * @author tgw
 * @date 2022/9/30
 * @describe
 */
fun main(array: Array<String>) {
    My_2().calculate()

    println("上面是加法题目都理解错了--")
    var l1 = Solution.ListNode(6).apply {
        next =Solution.ListNode(4).apply {
            next =Solution.ListNode(3).apply {
                next = null
            }
        }
    }
    var l2 = Solution.ListNode(5).apply {
        next =Solution.ListNode(6).apply {
            next =Solution.ListNode(4).apply {
                next = null
            }
        }
    }
    var num = Solution().addTwoNumbers(l1,l2)
    while (num != null){
        print("num:${num.`val`}")
        num = num.next
    }

    println("官方解法")
    var num2 = Solution().addTwoNumbers2(l1,l2)
    while (num2 != null){
        print("num2:${num2.`val`}")
        num2 = num2.next
    }
    println("\n官方解法模拟解释-引用之间的关系:")
//    cursor?.next = Solution.ListNode(sumVal % 10)
//    //将新对象，赋值给原cursor，这样就相当于cursor是新的值了，
//    // 但是root还保留最初cursor的引用
//    cursor = cursor?.next
    var a = My_2.NUM(5)
    var c = My_2.NUM(9)
    var b = a
    b.a = 6
     b = c
//    b=My_2.NUM(7)
    println("b:${b.a}")
    println("a:${a.a}")

}
private class My_2{
//    var nums = arrayOf(2,7,1,8,2,9)
//    var numA = arrayOf(2,4,3)
//    var numB = arrayOf(5,6,4)

//    var numA = arrayOf(9,9,9,9,9,9,9,9)
//    var numB = arrayOf(9,9,9,9)
    var numA = arrayOf(6,4,3)
    var numB = arrayOf(5,6,4)

     fun calculate():Long{
       var num = 0
         var multiply = 1
         for (i in numA.size-1 downTo 0) {
             num =num + numA[i]*multiply
             multiply = multiply*10
         }
         multiply =1
         for (i in numB.size-1 downTo 0) {
             num =num + numB[i]*multiply
             multiply = multiply*10
         }

         print("num:$num")
         print("num:${num.toString().reversed()}")
         return num.toLong()
    }

     class NUM(var a:Int){

      var next:NUM?=null

    }




}
private class Solution {

    /**
     *大概就是243+564 = 807
     * 结果要反过来的话=708
     *
     */
    fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
        var l1: ListNode? = l1
        var l2: ListNode? = l2
        val root = ListNode(0)
        var cursor: ListNode? = root
        var carry = 0
        while (l1 != null || l2 != null || carry != 0) {
            val l1Val = if (l1 != null) l1.`val` else 0
            val l2Val = if (l2 != null) l2.`val` else 0
            val sumVal = l1Val + l2Val + carry
            carry = sumVal / 10
            //创建一个新的对象
            cursor?.next = ListNode(sumVal % 10)
            println("前cursor:$cursor")
            println("root:$root")
            //将新对象，赋值给原cursor，这样就相当于cursor是新的值了，
            // 但是root还保留最初cursor的引用
            cursor = cursor?.next
            println("后cursor:$cursor")
            println("root:$root")

            if (l1 != null) l1 = l1.next
            if (l2 != null) l2 = l2.next
        }
        println("最终root:$root")
        return root.next
    }

    fun addTwoNumbers2(l1: ListNode?, l2: ListNode?): ListNode? {
        var l1: ListNode? = l1
        var l2: ListNode? = l2
        var root :ListNode?= null
        var cursor:ListNode?= null
        var carry = 0
        while (l1 != null || l2 != null || carry != 0) {
            val l1Val = if (l1 != null) l1.`val` else 0
            val l2Val = if (l2 != null) l2.`val` else 0
            val sumVal = l1Val + l2Val + carry
            if (root==null){
                cursor = ListNode(sumVal%10)
                root  = cursor
            }else{
                cursor?.next = ListNode(sumVal % 10)
                cursor = cursor?.next
            }
            carry = sumVal / 10


            if (l1 != null) l1 = l1.next
            if (l2 != null) l2 = l2.next
        }
        if (carry>0){
            cursor?.next = ListNode(carry)
        }
        return root
    }
    class ListNode(val `val`:Int){
         var next:ListNode? = null
    }
}


